package com.huaqin.mymap;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.overlayutil.PoiOverlay;
import com.baidu.mapapi.overlayutil.TransitRouteOverlay;
import com.baidu.mapapi.search.busline.BusLineResult;
import com.baidu.mapapi.search.busline.BusLineSearch;
import com.baidu.mapapi.search.busline.OnGetBusLineSearchResultListener;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.baidu.mapapi.search.share.OnGetShareUrlResultListener;
import com.baidu.mapapi.search.share.PoiDetailShareURLOption;
import com.baidu.mapapi.search.share.ShareUrlResult;
import com.baidu.mapapi.search.share.ShareUrlSearch;

public class MainActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SDKInitializer.initialize(getApplicationContext());
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// setContentView(R.layout.activity_main);

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(android.R.id.content, new PlaceholderFragment())
					.commit();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment implements
			View.OnClickListener {
		MapView mMapView = null;
		BaiduMap mBaiduMap = null;
		EditText mSearchContent = null;
		Button mSearchBtn = null;
		PoiSearch mPoiSearch = null;
		BusLineSearch mBusLineSearch = null;
		RoutePlanSearch mRoutePlanSearch = null;
		ShareUrlSearch mShareUrlSearch = null;

		public PlaceholderFragment() {
		}

		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			mSearchBtn = (Button) rootView.findViewById(R.id.search_btn);
			mSearchBtn.setOnClickListener(this);
			mSearchContent = (EditText) rootView
					.findViewById(R.id.search_content);
			// 获取地图控件引用
			mMapView = (MapView) rootView.findViewById(R.id.bmapView);
			mBaiduMap = mMapView.getMap();
			// 普通地图
			mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
			// 卫星地图
			// mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
			// 开启交通图
			// mBaiduMap.setTrafficEnabled(true);
			return rootView;
		}

		@Override
		public void onStart() {
			super.onStart();
			// 标注覆盖物-----------------------------------------------------------------
			// 定义Maker坐标点
			LatLng point = new LatLng(39.963175, 116.400244);
			// 构建Marker图标
			BitmapDescriptor bitmap = BitmapDescriptorFactory
					.fromResource(R.drawable.user_in_map);
			// 构建MarkerOption，用于在地图上添加Marker
			OverlayOptions option = new MarkerOptions().position(point).icon(
					bitmap);
			// 在地图上添加Marker，并显示
			mBaiduMap.addOverlay(option);
			// 弹出窗覆盖物--------------------------------------------------------------------------------
			// 创建InfoWindow展示的view
			// Button button = new Button(getActivity());
			// button.setBackgroundResource(R.drawable.btn_back);
			// // 定义用于显示该InfoWindow的坐标点
			// // LatLng pt = new LatLng(39.86923, 116.397428);
			// // 创建InfoWindow的点击事件监听者
			// OnInfoWindowClickListener listener = new
			// OnInfoWindowClickListener() {
			// public void onInfoWindowClick() {
			// // 添加点击后的事件响应代码
			// }
			// };
			// // 创建InfoWindow
			// InfoWindow mInfoWindow = new InfoWindow(button, point, listener);
			// // 显示InfoWindow
			// mBaiduMap.showInfoWindow(mInfoWindow);
			// POI检索---------------------------------------------------------------------------------------
			mPoiSearch = PoiSearch.newInstance();
			mPoiSearch.setOnGetPoiSearchResultListener(poiListener);
			mBusLineSearch = BusLineSearch.newInstance();
			mBusLineSearch
					.setOnGetBusLineSearchResultListener(busLineSearchResultListener);
			mRoutePlanSearch = RoutePlanSearch.newInstance();
			mRoutePlanSearch
					.setOnGetRoutePlanResultListener(getRoutePlanResultListener);
			mShareUrlSearch = ShareUrlSearch.newInstance();
			mShareUrlSearch.setOnGetShareUrlResultListener(listener);
		}

		OnGetShareUrlResultListener listener = new OnGetShareUrlResultListener() {
			public void onGetPoiDetailShareUrlResult(ShareUrlResult result) {
				// 分享POI详情
			}

			public void onGetLocationShareUrlResult(ShareUrlResult result) {
				// 分享位置信息
			}
		};
		OnGetRoutePlanResultListener getRoutePlanResultListener = new OnGetRoutePlanResultListener() {

			@Override
			public void onGetWalkingRouteResult(WalkingRouteResult arg0) {
				// 获取步行线路规划结果
			}

			@Override
			public void onGetTransitRouteResult(TransitRouteResult result) {
				// 获取公交换乘路径规划结果
				if (result == null
						|| result.error != SearchResult.ERRORNO.NO_ERROR) {
					Toast.makeText(getActivity(), "抱歉，未找到结果",
							Toast.LENGTH_SHORT).show();
				}
				if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
					// 起终点或途经点地址有岐义，通过以下接口获取建议查询信息
					// result.getSuggestAddrInfo()
					return;
				}
				// Log.d("wuliqing", result.getTaxiInfo().toString());
				if (result.error == SearchResult.ERRORNO.NO_ERROR) {
					TransitRouteOverlay overlay = new MyTransitRouteOverlay(
							mBaiduMap);
					mBaiduMap.setOnMarkerClickListener(overlay);
					overlay.setData(result.getRouteLines().get(0));
					overlay.addToMap();
					overlay.zoomToSpan();
				}
			}

			@Override
			public void onGetDrivingRouteResult(DrivingRouteResult arg0) {
				// 获取驾车线路规划结果
			}
		};
		OnGetBusLineSearchResultListener busLineSearchResultListener = new OnGetBusLineSearchResultListener() {

			@Override
			public void onGetBusLineResult(BusLineResult arg0) {

			}
		};
		OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() {
			public void onGetPoiResult(PoiResult result) {
				// 获取POI检索结果
				if (result == null
						|| result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
					return;
				}
				if (result.error == SearchResult.ERRORNO.NO_ERROR) {
					mBaiduMap.clear();
					// 创建PoiOverlay
					PoiOverlay overlay = new MyPoiOverlay(mBaiduMap);
					// 设置overlay可以处理标注点击事件
					mBaiduMap.setOnMarkerClickListener(overlay);
					// 设置PoiOverlay数据
					overlay.setData(result);
					// 添加PoiOverlay到地图中
					overlay.addToMap();
					overlay.zoomToSpan();
					String uid = "";
					for (PoiInfo poi : result.getAllPoi()) {
						if (poi.type == PoiInfo.POITYPE.BUS_LINE
								|| poi.type == PoiInfo.POITYPE.SUBWAY_LINE) {
							// 说明该条POI为公交信息，获取该条POI的UID
							uid = poi.uid;
							break;
						}
					}
					mShareUrlSearch
							.requestPoiDetailShareUrl(new PoiDetailShareURLOption()
							// UID：为POI的UID信息，可用POI检索获取
									.poiUid(uid));
					return;
				}
			}

			public void onGetPoiDetailResult(PoiDetailResult result) {
				// 获取Place详情页检索结果
			}
		};

		@Override
		public void onDestroy() {
			super.onDestroy();
			// 在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
			mMapView.onDestroy();
			mPoiSearch.destroy();
			mBusLineSearch.destroy();
			mRoutePlanSearch.destroy();
			mShareUrlSearch.destroy();
		}

		@Override
		public void onResume() {
			super.onResume();
			// 在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
			mMapView.onResume();
		}

		@Override
		public void onPause() {
			super.onPause();
			// 在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
			mMapView.onPause();
		}

		private class MyPoiOverlay extends PoiOverlay {
			public MyPoiOverlay(BaiduMap baiduMap) {
				super(baiduMap);
			}

			@Override
			public boolean onPoiClick(int index) {
				super.onPoiClick(index);
				return true;
			}
		}

		private class MyTransitRouteOverlay extends TransitRouteOverlay {

			public MyTransitRouteOverlay(BaiduMap arg0) {
				super(arg0);
			}

			@Override
			public boolean onRouteNodeClick(int arg0) {
				return super.onRouteNodeClick(arg0);
			}
		}

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.search_btn:
				// 查询地点
				mPoiSearch.searchInCity((new PoiCitySearchOption()).city("北京")
						.keyword("美食").pageNum(10));
				// 查询公交线路
//				PlanNode stNode = PlanNode.withCityNameAndPlaceName("北京", "龙泽");
//				PlanNode enNode = PlanNode.withCityNameAndPlaceName("北京", "西单");
//				mRoutePlanSearch.transitSearch((new TransitRoutePlanOption())
//						.from(stNode).city("北京").to(enNode));
				break;

			default:
				break;
			}
		}
	}

}
